<script setup>
// uni.navigateBack(2)
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { ref } from "vue";
import { orderpayState } from "@/services/order";

const type = ref("");
let setout = ref(null);
const order_id = ref("");
onLoad((e) => {
  if (e.order_id) {
    setout.value = setInterval(() => {
      getorderpayState(e.order_id);
    }, 1000);
  } else {
    type.value = "1";
  }
  order_id.value = e.order_id ? e.order_id : "";
});
onHide(() => {
  clearInterval(setout.value);
  // clearTimeout();
});

async function getorderpayState(orderid) {
  let res = await orderpayState({ order_id: orderid });
  if (res.data.state == 0) {
    type.value = 2;
  } else {
    type.value = 1;
  }
}

const gohome = () => {
  clearInterval(setout.value);
  // uni.navigateBack()
  uni.reLaunch({ url: "/pages/index/index" });
  // let pages = getCurrentPages();

  // 获取页面栈的长度，即页面跳转层级
  // let level = pages.length;

  // if (level > 1) {
  //   uni.navigateBack();
  // } else {
  //   uni.switchTab({
  //     url: "/pages/my/index",
  //   });
  // }
};
const goorder = () => {
  clearInterval(setout.value);
  // uni.navigateBack()

  if (order_id.value) {
    uni.reLaunch({ url: "/pages/order/orderdet/index?id=" + order_id.value });
  } else {
    uni.reLaunch({ url: "/pages/order/orderlist/index?status=1" });
  }

  // let pages = getCurrentPages();

  // 获取页面栈的长度，即页面跳转层级
  // let level = pages.length;

  // if (level > 1) {
  //   uni.navigateBack();
  // } else {
  //   uni.switchTab({
  //     url: "/pages/my/index",
  //   });
  // }
};
const goback = () => {
  // // uni.navigateBack()
  // clearInterval(setout.value);
  clearInterval(setout.value);
  let pages = getCurrentPages();

  // 获取页面栈的长度，即页面跳转层级
  let level = pages.length;

  if (level > 1) {
    uni.navigateBack();
  } else {
    uni.switchTab({
      url: "/pages/my/index",
    });
  }

  // uni.reLaunch({ url: "/pages/order/orderlist/index?status=-1" });
};
</script>
<template>
  <view class="paycontent">
    <!-- pay-err -->
    <view
      class="pay-hx"
      :class="type == 1 ? 'pay-hon' : type == 2 ? 'pay-err' : ''"
    >
    </view>
    <view class="pay-res" v-if="type == 1">
      <view class="pay-res-img">
        <image src="@/pages/upgrade/static/icon_successful.png" mode=""></image>
      </view>
      <view class="pay-res-name"> 提交成功 </view>
      <view class="pay-res-dec"> 下单成功,可前往订单查看下单信息 </view>
      <view class="pay-res-k">
        <view class="pay-res-see" @click="goorder"> 查看订单 </view>
        <view class="pay-res-bt" @click="gohome"> 返回首页 </view>
      </view>
    </view>

    <view class="pay-res" v-if="type == 2">
      <view class="pay-res-img">
        <image src="@/pages/upgrade/static/icon_error.png" mode=""></image>
      </view>
      <view class="pay-res-name"> 支付失败 </view>
      <view class="pay-res-dec"> 下单失败,可前往未支付订单支付 </view>
      <view class="pay-res-k">
        <view class="pay-res-see refresh-pay" @click="goback"> 返回 </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
